'use strict';


const db = uniCloud.database();
const dbCmd = db.command // 取指令

async function orderList(event){
	let orderAll = [],
			orderTodo = [],
      orderDone = [],
      orderCancel = []
	// 获取order
	let { memberId } = event
	await db.collection('order')
	.aggregate()
	.match({openid:memberId,status:dbCmd.lt(3)})
	.lookup({
    from: 'uni-id-users',
    localField: 'uid',
    foreignField: '_id',
    as: 'userList',
  })
  .end().then((res)=>{
		orderAll = res.data
	})
	
	await db.collection('order')
	.aggregate()
	.match({openid:memberId,status:0})
	.lookup({
	  from: 'uni-id-users',
	  localField: 'uid',
	  foreignField: '_id',
	  as: 'userList',
	})
	.end()
	.then((res)=>{
		orderTodo = res.data
	})
	
	await db.collection('order')
	.aggregate()
	.match({openid:memberId,status:1})
	.lookup({
	  from: 'uni-id-users',
	  localField: 'uid',
	  foreignField: '_id',
	  as: 'userList',
	})
	.end()
	.then((res)=>{
		orderDone = res.data
	})
	
	await db.collection('order')
	.aggregate()
	.match({openid:memberId,status:2})
	.lookup({
	  from: 'uni-id-users',
	  localField: 'uid',
	  foreignField: '_id',
	  as: 'userList',
	})
	.end()
	.then((res)=>{
		orderCancel = res.data
	})
	
	return {
		code: 0,
		msg:'',
		data: {orderAll,orderTodo,orderDone,orderCancel}
	}
}

exports.main = orderList;